package features.advance.demo;

/**
 * @author LIN
 * @date 2022-05-31 19:35
 */
public class TestDemo {
    /**
     * 给定一个排序数组和一个目标值，在数组中找到目标值，并返回其索引。如果目标不存在，返回他将要插入的位置索引
     * src_list:[-1,2,3,4] target 2
     *  res :1
     *  时间复杂度lgN
     * @param args
     */
    public static void main(String[] args) {
        // 数组越界
        int target = -2;
        int[] src = {-1,2,4,5,9};
        int res = findTarget(target,src);
        System.out.println(res);

    }

    public static int findTarget(int target,int[] src){

        int left = 0,right = src.length;
        int mid = 0;
        while(left<=right){
            mid = left + (right-left)/2;
            if(src[mid] == target){
                return mid;
            }else if(src[mid] > target){
                right = mid-1;
            }else {
                left = mid+1;
                if(left >= src.length){
                    return left;
                }
            }
        }
        return left;
    }

}
